package edu.pet.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.Specialties;

/**
 * Servlet implementation class SpecServlet
 */
public class SpecServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SpecServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		PetDAO dao = new PetDAO();
		List<Specialties> specList = new ArrayList<Specialties>();
		Specialties spec = new Specialties();
		if("add".equals(type)){
			spec.setName(request.getParameter("name"));
			dao.addSpec(spec);
			this.getServletContext().getRequestDispatcher("/servlet/SpecServlet?type=list").forward(request, response);
		}
		if("edit".equals(type)){
			spec.setId(Integer.parseInt(request.getParameter("id")));
			spec.setName(request.getParameter("name"));
			dao.editSpec(spec);
			this.getServletContext().getRequestDispatcher("/servlet/SpecServlet?type=list").forward(request, response);
		}
		if("list".equals(type)){
			specList = dao.findAllSpecialies();
			request.setAttribute("specList", specList);
			this.getServletContext().getRequestDispatcher("/user/manage/SpecList.jsp").forward(request, response);
		}
		if("del".equals(type)){
			int id = Integer.parseInt(request.getParameter("id"));
			if(dao.delSpec(id)){
				request.setAttribute("msg", "删除成功！");
			}else{
				request.setAttribute("msg", "该专长正在使用中！");
			}
			this.getServletContext().getRequestDispatcher("/servlet/SpecServlet?type=list").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
